package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.yunlang.zhangZhongFei.entity.Company;
import cn.yunlang.zhangZhongFei.entity.Settle;
import cn.yunlang.zhangZhongFei.mapper.CompanyMapper;
import cn.yunlang.zhangZhongFei.mapper.SettleMapper;
import cn.yunlang.zhangZhongFei.service.SettleService;
import cn.yunlang.zhangZhongFei.vo.response.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 公司入驻表 服务实现类
 * </p>
 *
 * @author ·
 * @since 2025-05-27 09:54:49
 */
@Service
public class SettleServiceImpl extends ServiceImpl<SettleMapper, Settle> implements SettleService {

    @Resource
    private CompanyMapper companyMapper;

    public R<IPage<Settle>> pageSettle(Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            return R.error(400, "分页参数不能为空");
        }
        IPage<Settle> page = this.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<Settle>().orderByDesc(Settle::getCreateTime));
        return R.success(page);
    }

    public R<Settle> getOne(Long settleId) {
        if (settleId == null) {
            return R.error(400, "ID不能为空");
        }
        Settle settle = this.getById(settleId);
        if (settle == null) {
            return R.error(404, "未找到入驻信息");
        }
        return R.success(settle);
    }

    @Transactional(rollbackFor = Exception.class)
    public R<String> allow(Long settleId, String type) {
        if (settleId == null) {
            return R.error(400, "ID不能为空");
        }
        if ("1".equals(type)) {
            return R.error(500, "权限不足，无法审批入驻信息。");
        }
        Settle settle = getById(settleId);
        if (settle == null) {
            return R.error(404, "未找到入驻信息");
        }
        // 公司名称查重
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Company::getCompanyName, settle.getCompanyName());
        if (companyMapper.selectCount(wrapper) > 0) {
            return R.error(400, "公司名称已存在");
        }
        Company company = new Company();
        BeanUtils.copyProperties(settle, company, "id", "createTime");
        int insert = companyMapper.insert(company);
        if (insert != 1) {
            return R.error(500, "操作失败");
        }
        boolean update = update(new LambdaUpdateWrapper<Settle>().set(Settle::getState, "1").eq(Settle::getState, "0").eq(Settle::getId, settleId));
        return update ? R.success("审批成功") : R.error(500, "审批失败");
    }

    public R<String> disAllow(Long settleId, String type) {
        if (settleId == null) {
            return R.error(400, "ID不能为空");
        }
        if ("1".equals(type)) {
            return R.error(500, "权限不足，无法审批入驻信息。");
        }
        boolean update = update(new LambdaUpdateWrapper<Settle>().set(Settle::getState, "2").eq(Settle::getState, "0").eq(Settle::getId, settleId));
        return update ? R.success("审批成功") : R.error(500, "审批失败");
    }
}
